const DataLoader = require('dataloader')
import { Answer } from '../../models'
import { groupData } from '../../lib/graphql'

export const answerLoaderByProgress = () =>
  new DataLoader(
    async keys => {
      const data = await Answer.findAll({
        where: { progressId: { $in: keys } },
        order: [['updatedAt', 'ASC']]
      })
      return groupData(keys, data, 'progressId')
    },
    { cacheKeyFn: key => key.toString() }
  )
